'use strict';

const { Controller } = require('egg');

class HomeController extends Controller {
  replyText(ctx, text) {
    ctx.body = {
      version: '2.1',
      response: {
        outputSpeech: {
          type: 'PlainText',
          text,
        },
      },
    };
  }

  delegate(ctx, slotToElicit, text) {
    ctx.body = {
      version: '2.1',
      response: {
        outputSpeech: {
          type: 'PlainText',
          text,
        },
        shouldEndSession: false,
        directives: [
          {
            type: 'Dialog.ElicitSlot',
            slotToElicit,
          },
        ],
      },
    };
  }

  async index() {
    const { ctx } = this;
    const { request } = ctx.request.body;
    const { dialogState } = request;
    // eslint-disable-next-line no-constant-condition
    if (dialogState === 'IN_PROGRESS' && false) {
      ctx.body = {
        version: '2.1',
        response: {
          directives: [
            {
              type: 'Dialog.Delegate',
            },
          ],
        },
      };
    } else {
      const { intent } = request;
      switch (intent.name) {
        case 'greeting':
          this.replyText(ctx, '你好！');
          break;
        case 'weather': {
          const { slots } = intent;
          if (!slots || !slots.chinacity) {
            this.delegate(ctx, 'chinacity', '您要查询哪个城市的天气？');
            break;
          }
          const city = slots.chinacity.normValue.replace('市', '');
          let date;
          if (slots.datetimeinvalid) date = new Date(JSON.parse(slots.datetimeinvalid.normValue).datetime);
          else date = new Date();
          try {
            const result = await ctx.service.fly.getWeather(city, date);
            this.replyText(ctx, result);
          } catch (e) {
            this.replyText(ctx, e.message);
          }
          break;
        }
        case 'notice': {
          if (!intent.slots || !intent.slots.datetime) {
            this.delegate(ctx, 'datetime', '在什么时候提醒您？');
            break;
          }
          if (!intent.slots || !intent.slots.wildcard) {
            this.delegate(ctx, 'wildcard', '提醒您什么事情？');
            break;
          }
          const date = new Date(JSON.parse(intent.slots.datetime.normValue).suggestDatetime);
          const str = intent.slots.wildcard.normValue;
          const obj = {
            type: 'notice',
            date,
            str,
          };
          this.replyText(ctx, JSON.stringify(obj));
          break;
        }
        case 'article': {
          if (!intent.slots || !intent.slots.datetimeinvalid) {
            this.delegate(ctx, 'datetimeinvalid', '什么时候发表的文章？');
            break;
          }
          const date = new Date(JSON.parse(intent.slots.datetimeinvalid.normValue).suggestDatetime);
          const obj = {
            type: 'article',
            date,
          };
          this.replyText(ctx, JSON.stringify(obj));
          /* try {
            const reply = await ctx.service.news.pick(date);
            console.log(reply);
            this.replyText(ctx, reply);
          } catch (e) {
            this.replyText(ctx, e.message);
          } */
          break;
        }
        case 'search': {
          if (!intent.slots || !intent.slots.actor) {
            this.delegate(ctx, 'actor', '搜索哪个关键词？');
            break;
          }
          const str = intent.slots.actor.normValue;
          const obj = {
            type: 'search',
            str,
          };
          this.replyText(ctx, JSON.stringify(obj));
          /* try {
            const reply = await ctx.service.news.search(str);
            console.log(reply);
            this.replyText(ctx, reply);
          } catch (e) {
            this.replyText(ctx, e.message);
          } */
          break;
        }
        default:
          this.replyText(ctx, '该功能尚未支持，试试：\n明天广州市天气怎么样；\n明天九点提醒我起床；\n搜索信息通信；\n找出两天前发的文章');
          break;
      }
    }
  }
}

module.exports = HomeController;
